package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/17.
 *
 */
@Data
public class ButcherAdmissionInspection implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = 3804569167392109262L;

    /**
     * 主键
     */
    private Long id;

    /**
     * 编号
     */
    private String number;

    /**
     * 状态，0：未提交，1：已生效，
     */
    private Integer status;

    /**
     * 操作人ID
     */
    private Long userId;

    /**
     * 操作人名称
     */
    private String userName;

    /**
     * 生成时间
     */
    private Date date;

    /**
     * 货主
     */
    private String owner;

    /**
     * 货主电话
     */
    private String ownerMobile;

    /**
     * 屠宰场id
     */
    private Long butcherFactoryId;

    /**
     * 屠宰场名称
     */
    private String butcherFactoryName;

    /**
     * 《无害化处理通知单》id
     */
    private Long harmlessId;

    /**
     * 《无害化处理通知单》编号
     */
    private String harmlessNumber;

    /**
     * 关联产地检疫单据ID
     */
    private Long breedReportId;

    /**
     * 关联产地检疫申报号
     */
    private String breedReportNumber;

    /**
     * 关联屠宰检疫单ID
     */
    private Long butcherReportId;

    /**
     * 关联屠宰检疫单号
     */
    private String butcherReportNumber;

    /**
     * 是否本地 -1 外市 ； 1 本市
     */
    private Integer isLocal;

    /**
     * 该单据是否被选择,-1:未选中;1:选中
     */
    private Integer butcherSelected;

    /**
     *  产品类型：
     */
    private Integer productType;

    /**
     * 单位
     */
    private Integer unit;

    /**
     *  数量
     */
    private Integer quantity;

    /**
     * 来源id
     */
    private Long sourceId;

    /**
     * 来源名称
     */
    private String sourceName;

    /**
     * 耳标号
     */
    @JsonIgnore
    private String earTagNumber;


    /**
     * 耳标号list
     */
    private List<String> earTagNumbers;

    /**
     *
     */
    private String remainderEar;

    /**
     *
     */
    private List<String> remainderEars;

    /**
     * 临床检查 --待定
     */
    private Integer clinicalExamination;

    /**
     * 是否佩戴规定的畜禽标识-1:否;1:是;默认1
     */
    private Integer hasAnimalIdentify;

    /**
     * 检疫证号
     */
    private String quarantineCertNumber;

    /**
     *
     */
    private Map<String ,Map<String , String>> quarantineCertPhotoMap;

    /**
     * 检疫证号照片
     */
    @JsonIgnore
    private String quarantineCertPhotoJson;

    /**
     * 合格数
     */
    private Integer qualifiedQuantity;

    /**
     * 不合格数
     */
    private Integer unqualifiedQuantity;

    /**
     * 可用于屠宰的数量，即本单据的未屠宰数
     */
    private Integer remainder;

    /**
     * 已经屠宰的数量
     */
    private Integer slaughterings;

    /**
     *
     */
    private Map<String , Float> extra;

    /**
     * 扩展信息 瘦肉精检测抽样数,默认值为0 盐酸克伦特罗-阳性,默认值为0 莱克多巴胺-阳性,默认值为0 沙丁胺醇-阳性,默认值为0
     */
    @JsonIgnore
    private String extraJson;

    /**
     * 单据标志位 1：正常，-1：删除
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setQuarantineCertPhotoJson(String quarantineCertPhotoJson) {
        this.quarantineCertPhotoJson = quarantineCertPhotoJson;
        if(Strings.isNullOrEmpty(quarantineCertPhotoJson)){
            this.quarantineCertPhotoMap = Maps.newHashMap();
        }else{
            try {
                this.quarantineCertPhotoMap = objectMapper.readValue(quarantineCertPhotoJson, new TypeReference<Map<String , Map<String , String>>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setQuarantineCertPhotoMap(Map<String , Map<String , String>> quarantineCertPhotoMap){
        this.quarantineCertPhotoMap = quarantineCertPhotoMap;
        if(quarantineCertPhotoMap == null || quarantineCertPhotoMap.isEmpty()){
            this.quarantineCertPhotoJson = null;
        }else {
            try {
                this.quarantineCertPhotoJson = objectMapper.writeValueAsString(quarantineCertPhotoMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }

    public void setExtraJson(String extraJson){
        this.extraJson = extraJson;

        if(Strings.isNullOrEmpty(extraJson)){
            this.extra = Maps.newHashMap();
        }else {
            try {
                this.extra = objectMapper.readValue(extraJson, new TypeReference<Map<String , Float>>() {});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setExtra(Map<String , Float> extra){
        this.extra = extra;

        if(extra == null || extra.isEmpty()){
            this.extraJson = null;
        }else {
            try {
                this.extraJson = objectMapper.writeValueAsString(extra);
            } catch (JsonProcessingException e) {
                //ignore this fuck JsonProcessingException
            }
        }
    }



    public void setEarTagNumber(String earTagNumber){
        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = new ArrayList<>();
        }else {
            try {
                this.earTagNumbers =
                        objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarTagNumbers(List<String> earTagNumbers){
        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()){
            this.earTagNumber = null;
        }else {
            try {
                this.earTagNumber =
                        objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

    public void setRemainderEar(String remainderEar){
        this.remainderEar = remainderEar;

        if(Strings.isNullOrEmpty(remainderEar)){
            this.remainderEars = new ArrayList<>();
        }else {
            try {
                this.remainderEars =
                        objectMapper.readValue(remainderEar, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setRemainderEars(List<String> remainderEars){
        this.remainderEars = remainderEars;

        if(remainderEars == null || remainderEars.isEmpty()){
            this.remainderEar = "";
        }else {
            try {
                this.remainderEar =
                        objectMapper.writeValueAsString(remainderEars);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }
}
